﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace TransportLayer.Manager
{
    public partial class Loader : Form
    {
        private Engine engine;
        
        public Loader(Engine engine)
        {
            InitializeComponent();
            this.engine = engine;
        }


        public void DeleteNetwork()
        {
            progressBar.Value = 0;
            Refresh();
            label.Text = "Usuwanie procesów węzłów...";
            engine.StopAlive();
            progressBar.Value = 10;
            Refresh();
   
            progressBar.Value = 90;
            Refresh();
            engine.Clean();
            progressBar.Value = 100;
            Refresh();
            
            this.Close();
        }

        public bool LoadNetwork(string filename)
        {
            label.Text = "Opening the file...";
            progressBar.Value = 10;
            Refresh();
            TransportLayer.Network network;
            try { network = TransportLayer.Network.fopen(filename); }
            catch (InvalidOperationException e)
            {
                MessageBox.Show(e.Message, "Wrong input file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
                return false;
            }

            label.Text = "Creating nodes...";
            progressBar.Value = 20;
            Refresh();
            Random random = new Random();
            Dictionary<int, Process> processes = new Dictionary<int, Process>();
            foreach (TransportLayer.Node node in network.Nodes)
            {
                string tempfile = "tmp" + random.Next() + ".xml";
                node.save(tempfile);
                Process process = new Process();
                process.StartInfo.FileName = "TransportLayer.ExecNode.exe";
                process.StartInfo.Arguments = tempfile + " " + engine.Port +" " + 1237;
                process.Start();
                processes.Add(node.Id, process);
                progressBar.Value += (40 / network.Nodes.Count);
                
                Refresh();
                
            }

            label.Text = "Waiting for agents...";
            Refresh();
            while (engine.ConnectedNodes < network.Nodes.Count)
            { Thread.Sleep(50); }
            engine.PharseProcessDictonary(processes);

            label.Text = "Creating links...";
            progressBar.Value = 60;
            Refresh();
            foreach (TransportLayer.Link link in network.Links)
            {
                engine.AddLink(link);
                progressBar.Value += (40 / network.Links.Count);
                Refresh();
            }
            Thread.Sleep(1000);
            engine.KeepAlive();
            engine.HideAllNodes();
            engine.NetworkName = network.Name;
            this.Close();

            return true;
        }
        private void Loader_Load(object sender, EventArgs e)
        {

        }
    }
}
